"""
成品管理页面

管理制作完成的成品的页面
"""

import tkinter as tk
from tkinter import ttk
from pathlib import Path
import sys

# 添加项目根目录到路径
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root))

from src.gui.main_app import BasePage


class OutputManagementPage(BasePage):
    """成品管理页面"""
    
    def create_page(self):
        """创建页面内容"""
        self.frame = ttk.Frame(self.parent)
        
        # 页面标题
        header_frame = ttk.Frame(self.frame)
        header_frame.pack(fill="x", pady=(0, 20))
        
        title_label = ttk.Label(
            header_frame,
            text="🎬 成品管理",
            style='Title.TLabel'
        )
        title_label.pack(side="left")
        
        # 工具栏
        toolbar_frame = ttk.Frame(self.frame)
        toolbar_frame.pack(fill="x", pady=(0, 20))
        
        ttk.Button(
            toolbar_frame,
            text="🎬 新建成品",
            command=self.create_output
        ).pack(side="left", padx=(0, 10))
        
        ttk.Button(
            toolbar_frame,
            text="📤 批量导出",
            command=self.batch_export
        ).pack(side="left")
        
        # 成品列表
        list_frame = ttk.LabelFrame(self.frame, text="成品列表", padding="10")
        list_frame.pack(fill="both", expand=True)
        
        # 创建成品列表
        columns = ('name', 'product', 'template', 'duration', 'status', 'created')
        self.output_tree = ttk.Treeview(list_frame, columns=columns, show='headings', height=12)
        
        # 设置列标题
        headers = {
            'name': '成品名称',
            'product': '关联商品',
            'template': '使用模板',
            'duration': '时长',
            'status': '状态',
            'created': '创建时间'
        }
        
        for col, title in headers.items():
            self.output_tree.heading(col, text=title)
            self.output_tree.column(col, width=130)
        
        # 滚动条
        scrollbar = ttk.Scrollbar(list_frame, orient="vertical", command=self.output_tree.yview)
        self.output_tree.configure(yscrollcommand=scrollbar.set)
        
        # 布局
        self.output_tree.pack(side="left", fill="both", expand=True)
        scrollbar.pack(side="right", fill="y")
        
        # 加载示例数据
        self.load_sample_outputs()
    
    def load_sample_outputs(self):
        """加载示例成品数据"""
        outputs = [
            ("时尚T恤宣传片", "时尚T恤-SKU001", "时尚展示模板", "00:15", "已完成", "2024-01-15"),
            ("运动鞋介绍视频", "运动鞋-SKU002", "产品介绍模板", "00:30", "制作中", "2024-01-15"),
            ("连衣裙展示", "夏季连衣裙-SKU004", "时尚展示模板", "00:15", "已发布", "2024-01-14"),
            ("商务衬衫广告", "商务衬衫-SKU005", "产品介绍模板", "00:25", "待审核", "2024-01-14"),
            ("休闲裤宣传", "休闲裤-SKU003", "快闪文字模板", "00:10", "已完成", "2024-01-13")
        ]
        
        for output in outputs:
            # 根据状态设置标签
            status_tags = {
                '已完成': 'completed',
                '制作中': 'processing',
                '已发布': 'published',
                '待审核': 'pending'
            }
            tag = status_tags.get(output[4], '')
            
            self.output_tree.insert('', 'end', values=output, tags=(tag,))
        
        # 配置标签颜色
        self.output_tree.tag_configure('completed', background='#d5f4e6')
        self.output_tree.tag_configure('processing', background='#fff2cc')
        self.output_tree.tag_configure('published', background='#cce5ff')
        self.output_tree.tag_configure('pending', background='#ffe6cc')
    
    def create_output(self):
        """创建新成品"""
        tk.messagebox.showinfo("提示", "新建成品功能开发中...")
    
    def batch_export(self):
        """批量导出"""
        tk.messagebox.showinfo("提示", "批量导出功能开发中...")
